import { showMyOrder, transferOrder } from "@/api/index";
import { router } from "@/util/index";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    enable: false,
    orderList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad() {
    const data = await showMyOrder();
    data.forEach((element) => {
      element.endId = element._id.slice(-8);
      element.dateTime = new Date(element.createdAt).toLocaleString();
    });
    this.setData({
      orderList: data,
    });
  },
  onOrderResolve(e) {
    router.navigateTo("../order_resolve/index", {
      query: {
        id: e.currentTarget.dataset.id,
      },
    });
  },
  async onOrderTransfer(e) {
    let { id } = e.currentTarget.dataset;
    await transferOrder(id);
    const newData = this.data.orderList.filter((i) => i._id !== id);
    this.setData({
      orderList: newData,
    });
  },
  onOrderContact(e) {
    const { avatar, uid, nickname } = e.target.dataset;
    router.navigateTo("/pages/im/index", {
      query: {
        peerId: uid,
        peerNickname: nickname,
        peerAvatar: avatar,
        isVirtualPeer: false,
        isCon: true,
      },
    });
  },
  onScroll(e) {
    const { scrollTop } = e.detail;
    this.setData({ scrollTop });
  },
  async onRefresh() {
    this.setData({ enable: true });
    const data = await showMyOrder();
    data.forEach((element) => {
      element.endId = element._id.slice(-8);
      element.dateTime = new Date(element.createdAt).toLocaleString();
    });
    this.setData({
      orderList: data,
      enable: false,
    });
  },
});
